import { getCategoryTableApi } from '@/api/cms/category/category'
import { CategoryListParam } from '@/api/cms/category/CategoryModel'
import { StatusCode } from '@/http/request'
import { onMounted, reactive, nextTick, ref } from 'vue'

export default function useCategoryTable() {
    // 定义列表查询的参数
    const listParam = reactive<CategoryListParam>({
        name: '',
        status: '1'
    })
    // 表格的高度
    const tableHeight = ref(0)
    // 表格数据
    const categoryTable = reactive({
        list: []
    })
    // 获取表格数据
    const getCategoryTable = async () => {
        let res = await getCategoryTableApi(listParam)
        if (res && res.code == StatusCode.Success) {
            categoryTable.list = res.data
        }
    }

    // 搜索
    const searchBtn = () => {
        getCategoryTable()
    }
    // 重置
    const resetBtn = () => {
        listParam.name = ''
        listParam.status = '1'
        getCategoryTable()
    }

    // 首次加载
    onMounted(() => {
        getCategoryTable()
        nextTick(() => {
            tableHeight.value = window.innerHeight - 180
        })
    })

    return {
        listParam,
        resetBtn,
        searchBtn,
        tableHeight,
        categoryTable,
        getCategoryTable
    }
}
